﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OgilvyOne.UKNow.Model;
using ECCIT.Data;
using System.Data;
namespace OgilvyOne.UKNow.DAL
{
  public  class HomeVideoDAL
    {
      string   tablename = "uknow_home_video";
      DataTable dt = new DataTable();
      public bool Add(HomeVideo hv)
      {
          string sqlcmd = String.Format("insert into {0} values('{1}','{2}','{3}','{4}','{5}','{6}') ",tablename,hv.Id,hv.VideoName,hv.VideoUrl,hv.VideoDesc,hv.InsertTime.ToString(),hv.UpdateTime.ToString());
          return fnData(sqlcmd, "add");
          
      }

      public bool Update(HomeVideo hv,string uid)
      {
          string sqlcmd = String.Format("update {0} set id='{1}',videoname='{2}',VideoUrl='{3}',VideoDesc='{4}',InsertTime='{5}',UpdateTime='{6}'where id='{7}' ", tablename, hv.Id.Trim(), hv.VideoName.Trim(), hv.VideoUrl, hv.VideoDesc, hv.InsertTime.ToString(), hv.UpdateTime.ToString(),uid);
          return fnData(sqlcmd, "update");
      }
      public List<HomeVideo> GetAllModList() 
      {
          string sqlcmd = String.Format("select * from {0} ",tablename);
          fnData(sqlcmd,"get");
          return DataToModList();

      }
      private  bool fnData(string sqlcmd,string fntype)
      {

          if (fntype=="add")
          {
              try
              {
                  DBHelper.ExecuteNonQuery(sqlcmd, null);
                  return true;
              }
              catch (Exception)
              {

                  return false;
              }
                
             
          }
          else if (fntype == "update")
          {
              //try
              //{
                  DBHelper.ExecuteNonQuery(sqlcmd);
                  return true;
              //}
              //catch (Exception)
              //{

              //    return false;
              //}

          }
          else if (fntype == "get")
          {
              try
              {
                  dt = DBHelper.ExecuteDataSet(sqlcmd, null).Tables[0];
                  return true;
              }
              catch (Exception)
              {

                  return false;
              }


          }
          else {
              return false;
          }

           
      }
      private List<HomeVideo> DataToModList()
      {
           
          List<HomeVideo>   lhv =new List<HomeVideo> ();
          if (dt.Rows.Count != 0)
          {
              #region
              foreach (DataRow dr in dt.Rows)
              {
                  HomeVideo hv = new HomeVideo();
                  hv.Id = dr["Id"].ToString();
                  hv.VideoName = dr["VideoName"] != null ? dr["VideoName"].ToString() : "";
                  hv.VideoDesc = dr["VideoDesc"] != null ? dr["VideoDesc"].ToString() : "";
                  hv.VideoUrl = dr["VideoUrl"] != null ? dr["VideoUrl"].ToString() : "";
                  if (dr["InsertTime"] != null)
                  {
                      hv.InsertTime = Convert.ToDateTime(dr["InsertTime"]);
                  };
                  if (dr["UpdateTime"] != null)
                  {
                      hv.UpdateTime = Convert.ToDateTime(dr["UpdateTime"]);
                  };
                  lhv.Add(hv);


              }
              #endregion
              return lhv;
          }
          else { return null; }

          }
      }
    
}
